package com.zsw.alancomicserver.controller.miniProgram;

import com.zsw.alancomicserver.repository.BookItemRepository;
import com.zsw.alancomicserver.repository.BookRepository;
import com.zsw.alancomicserver.repository.po.BookItemPO;
import com.zsw.alancomicserver.repository.po.BookPO;
import com.zsw.alancomicserver.util.DateUtil;
import com.zsw.alancomicserver.vo.BookItemVO;
import com.zsw.alancomicserver.vo.BookVO;
import com.zsw.alancomicserver.vo.DirectionItemVO;
import com.zsw.alancomicserver.vo.ResponseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 小程序核心功能API
 */
@RestController
@RequestMapping("/mini")
public class CoreApiController {

    @Resource
    private BookItemRepository bookItemRepository;

    @Resource
    private BookRepository bookRepository;

    /**
     * 获取首页数据
     */
    @GetMapping("/index")
    public ResponseVO<List<BookItemVO>> index() {
        ResponseVO<List<BookItemVO>> res = new ResponseVO<>();

        List<BookItemPO> bookItemPOS = bookItemRepository.queryLatest20();
        if (!CollectionUtils.isEmpty(bookItemPOS)) {
            res.setData(bookItemPOS.stream().map(this::convertBookItemVO).collect(Collectors.toList()));
        }

        return res;
    }

    /**
     * 详情页
     */
    @GetMapping("/getByIdx")
    public ResponseVO<BookItemVO> getByIdx(@RequestParam("idx") String idx) {
        ResponseVO<BookItemVO> res = new ResponseVO<>();
        Optional.ofNullable(bookItemRepository.queryByBookItemId(Long.valueOf(idx))).ifPresent(po -> {
            res.setData(convertBookItemVO(po));
        });
        return res;
    }

    /**
     * 书架
     */
    @GetMapping("/bookShelf")
    public ResponseVO<List<BookVO>> bookShelf() {
        ResponseVO<List<BookVO>> res = new ResponseVO<>();

        List<BookPO> bookPOList = bookRepository.queryAll();
        if (!CollectionUtils.isEmpty(bookPOList)) {
            res.setData(bookPOList.stream().map(this::convertBookVO).collect(Collectors.toList()));
        }

        return res;
    }

    /**
     * 目录
     */
    @GetMapping("/direction")
    public ResponseVO<List<DirectionItemVO>> direction(@RequestParam ("bookIdx") String bookIdx) {
        ResponseVO<List<DirectionItemVO>> res = new ResponseVO<>();

        List<BookItemPO> bookItemPOS = bookItemRepository.queryByBookId(Long.valueOf(bookIdx));
        if (!CollectionUtils.isEmpty(bookItemPOS)) {
            res.setData(bookItemPOS.stream().map(this::convertDirectionVO).collect(Collectors.toList()));
        }

        return res;
    }

    private DirectionItemVO convertDirectionVO(BookItemPO bookItemPO) {
        DirectionItemVO directionItemVO =  new DirectionItemVO();
        directionItemVO.setIdx(bookItemPO.getBookItemId());
        directionItemVO.setTitle(bookItemPO.getBookItemTitle());
        return directionItemVO;
    }

    private BookVO convertBookVO(BookPO po) {
        BookVO bookVO = new BookVO();
        BeanUtils.copyProperties(po, bookVO);
        bookVO.setCreateTime(DateUtil.toDateString(po.getCreateTime()));
        bookVO.setUpdateTime(DateUtil.toDateString(po.getUpdateTime()));
        return bookVO;
    }

    private BookItemVO convertBookItemVO(BookItemPO po) {
        BookItemVO bookItemVO = new BookItemVO();
        bookItemVO.setIdx(po.getBookItemId());
        bookItemVO.setContent(po.getContent());
        bookItemVO.setDatetime(DateUtil.toDateString(po.getUpdateTime()));
        bookItemVO.setNickname(po.getBookItemTitle());
        bookItemVO.setComicUrl(po.getBookItemImgs());
        bookItemVO.setHeadImgUrl(po.getHeadImgUrl());
        return bookItemVO;
    }
}
